package
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    
    public class Movinggrid extends Sprite
    {
        private var _grid:Array;
        private var _overallScale:Number = 20;
        private var _maxScale:Number = .5;
        private var _minScale:Number = .5;
        private var _maxDist:Number = 200;
        
        private var amount:int = 10; 
        
        private var _holder:Sprite = new Sprite();
        
        public function Movinggrid()
        {
            addChild(_holder);
            _holder.scaleY = _holder.scaleX = _overallScale;
            setupGrid();
            setupStage();
            addEventListener(Event.ENTER_FRAME, update);
        }
        
        private function setupGrid():void
        {
            _grid = [];
            var gridRow:Array;
            var i:int;
            var j:int;
            var box:Box;
            for(i=0;i<amount;i++)
            {
                gridRow = [];
                for(j=0;j<amount;j++)
                {
                    box = new Box(true);
                    box.y = i;
                    box.x = j;
                    _holder.addChild(box);
                    gridRow[j]= box;
                }
                _grid[i]=gridRow;
            }
        }
        
        private function setupStage():void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
        }
        
        private function update(event:Event):void
        {
            var distList:Array = []
            
            var i:int;
            var box:Box;
            var dist:Number;
            var dx:Number;
            var dy:Number;
            
            var isHor:Boolean = false;
            var dirH:int = 1;
            var dirV:int = 1;
            
            var length:int = 1;
            
            var col:int = mouseX/(_overallScale);
            var row:int = mouseY/(_overallScale);
            var step:int = 0;
            var counter:int = 0;
            var total:int = amount*amount;
            while(counter<total)
            {
                for(i=0;i<length;i++)
                {
                    if(col>=0 && col<amount && row>=0 && row<amount)
                    {
                        box = _grid[row][col];
                        _holder.setChildIndex(box,_holder.numChildren-counter-1);
                        
                        dx = mouseX-(col*_overallScale);
                        dy = mouseY-(row*_overallScale);
                        dist = Math.sqrt((dx*dx)+(dy*dy));
                        if(dist<_maxDist)
                        {
                            box.shape.scaleX = box.shape.scaleY = (1-Math.cos((90-(90*(dist/_maxDist)))*(Math.PI/180)))*_maxScale+_minScale;
                            box.shape.rotation = (box.shape.scaleX-1)*180;
                        } else {
                            box.shape.scaleX = box.shape.scaleY = _minScale;
                            box.shape.rotation = 0;
                        }
                        counter++;
                    }
                    
                    if(isHor)
                    {
                        col += dirH*1;
                    } else {
                        row += dirV*1;
                    }
                }
                if(isHor)
                {
                    dirH *= -1;
                } else {
                    dirV *= -1;
                }
                isHor = !isHor;
                step++;
                if(step%2==0) length++;
            }
        }
    }
}
import flash.display.Shape;
import flash.display.Sprite;
    
class Box extends Sprite
{
    public var shape:Shape = new Shape();
    private var _color:Number = 0x333333;
    private var _borderColor:Number = 0;
    public var size:int = 1;
            
    public function Box(autoRender:Boolean=false)
    {
        mouseEnabled = false;
        addChild(shape);
        if(autoRender) draw();
    }
        
    public function draw():void
    {
        var pos:Number = size/2;
        _color = Math.random()*0xFFFFFF;
        shape.graphics.beginFill(_color,1);
        shape.graphics.drawRect(-pos,-pos,size,size);
        shape.graphics.endFill();
        shape.x = shape.y = pos;
    }
        
    public function destroy():void
    {
        if(parent) parent.removeChild(this);
    }
}